//
//  SAGlobalSettingsSectionHeader.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/9.
//
#import "SAGlobalSettingsSectionHeader.h"

@interface SAGlobalSettingsSectionHeader()

@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UIImageView *printerImageV;
@property (nonatomic, strong) UILabel *printerNameLab;
@property (nonatomic, strong) UILabel *printerStatusLab;
@property (nonatomic, strong) UIImageView *arrowIcon;
@property (nonatomic, strong) UIView *dotView;
@property (nonatomic, strong) UIButton *printBtn;
@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UILabel *printNumLab;

@end

@implementation SAGlobalSettingsSectionHeader

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

- (void)refreshHeaderWithPrinterName:(NSString *)printerName printerStatus:(NSString *)printerStatus {
    self.printerNameLab.text = printerName;
    self.printerStatusLab.text = printerStatus;
    if ([printerStatus isEqualToString:@"打印机已连接"]) {
        self.dotView.backgroundColor = [UIColor greenColor];
        [self.printBtn setTitle:@"断开连接" forState:UIControlStateNormal];
    }else if ([printerStatus isEqualToString:@"打印机连接失败"]){
        self.dotView.backgroundColor = kRedColor;
        [self.printBtn setTitle:@"重新连接" forState:UIControlStateNormal];
    }else {
        self.dotView.backgroundColor = kRedColor;
        [self.printBtn setTitle:@"连接" forState:UIControlStateNormal];
    }
}

#pragma mark - event

- (void)printAction {
    if (self.printerStatusBlock) {
        self.printerStatusBlock();
    }
}

#pragma mark - initUI

- (void)initUI {
    
    [self.contentView addSubview:self.container];
    [self.contentView addSubview:self.printNumLab];
    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.equalTo(self.contentView.mas_height).multipliedBy(74.0 / 114.0);
    }];
    
    [self.printNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.container addSubview:self.printerImageV];
    [self.container addSubview:self.printerNameLab];
    [self.container addSubview:self.arrowIcon];
    [self.container addSubview:self.printerStatusLab];
    [self.container addSubview:self.dotView];
    [self.container addSubview:self.printBtn];
    [self.container addSubview:self.lineView];
    
    [self.printerImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(37);
        make.top.mas_equalTo(8.5);
        make.size.mas_equalTo(CGSizeMake(44.5, 44.5));
    }];
    
    [self.printerNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.printerImageV.mas_bottom).with.offset(4);
        make.centerX.equalTo(self.printerImageV);
    }];
    
    [self.arrowIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-30);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.dotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.printerImageV.mas_right).with.offset(10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(7.0, 7.0));
    }];
    
    [self.printerStatusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dotView.mas_right).with.offset(5);
        make.centerY.mas_equalTo(0);
    }];
    
    [self.printBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(80);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
        
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];

}

#pragma mark - lazy init

- (UIView *)container {
    if (!_container) {
        _container = [[UIView alloc] init];
        _container.backgroundColor = [UIColor whiteColor];
    }
    return _container;
}

- (UIImageView *)printerImageV {
    if (!_printerImageV) {
        _printerImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"printer_icon"]];
    }
    return _printerImageV;
}

- (UILabel *)printerNameLab {
    if (!_printerNameLab) {
        _printerNameLab = [[UILabel alloc] init];
        _printerNameLab.textColor = kColor(51, 51, 51, 1.0);
        _printerNameLab.font = [UIFont systemFontOfSize:11.0];
    }
    return _printerNameLab;
}

- (UILabel *)printerStatusLab {
    if (!_printerStatusLab) {
        _printerStatusLab = [[UILabel alloc] init];
        _printerStatusLab.font = [UIFont systemFontOfSize:16.0];
        _printerStatusLab.textColor = kBlackColor;
        _printerStatusLab.text = @"打印机未连接";
    }
    return _printerStatusLab;
}

- (UIImageView *)arrowIcon {
    if (!_arrowIcon) {
        _arrowIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SA_arrow3"]];
    }
    return _arrowIcon;
}

- (UIView *)dotView {
    if (!_dotView) {
        _dotView = [[UIView alloc] init];
        _dotView.backgroundColor = kRedColor;
        _dotView.layer.cornerRadius = 3.5;
    }
    return _dotView;
}

- (UIButton *)printBtn {
    if (!_printBtn) {
        _printBtn  =[UIButton buttonWithType:UIButtonTypeCustom];
        _printBtn.backgroundColor = kBuleColor;
        [_printBtn setTitle:@"连接" forState:UIControlStateNormal];
        _printBtn.titleLabel.font = kFont(16);
        _printBtn.layer.cornerRadius = kScaleNum(5);
        _printBtn.layer.masksToBounds = YES;
        [_printBtn addTarget:self action:@selector(printAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _printBtn;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLineColor;
    }
    return _lineView;
}

- (UILabel *)printNumLab {
    if (!_printNumLab) {
        _printNumLab = [[UILabel alloc] init];
        _printNumLab.font = [UIFont systemFontOfSize:20.0];
        _printNumLab.textColor = kColor(51, 51, 51, 1);
        _printNumLab.text = @"条码打印机参数设置";
    }
    return _printNumLab;
}

@end

